package org.example.likou150.array;

/*
* https://leetcode.cn/problems/integer-to-roman/description/?envType=study-plan-v2&envId=top-interview-150
* */
public class IntToRoman {
    public static void main(String[] args) {
        IntToRoman intToRoman=new IntToRoman();
        System.out.println(intToRoman.intToRoman(1));
    }

    public String intToRoman(int num) {
        StringBuilder sb=new StringBuilder(20);
        int tax=10;
        while(num*10>=tax){
            int i=(num % tax) / (tax/10);
            if(i==0){
            } else if(i==9){
                sb.insert(0, cover(10 * tax / 10));
                sb.insert(0, cover(tax / 10));
            }else if(i==5){
                sb.insert(0, cover(5 * tax/10));
            }
            else if(i==4){
                sb.insert(0, cover(5 * tax / 10));
                sb.insert(0, cover(tax / 10));
            } else if(i > 5){
                for (int j=0;j<i-5;j++){
                    sb.insert(0, cover(tax / 10));
                }
                sb.insert(0, cover(5 * tax / 10));
            } else {
                for (int j=0;j<i;j++){
                    sb.insert(0, cover(tax / 10));
                }
            }
            tax*=10;
        }
        return sb.toString();
    }

    public String cover(int num){
        switch (num){
            case 1 :
               return "I";
            case 5 :
                return "V";
            case 10 :
                return "X";
            case 50 :
                return "L";
            case 100 :
                return "C";
            case 500 :
                return "D";
            case 1000 :
                return "M";
            default:
                throw new RuntimeException("不直到传的什么玩意");
        }
    }
}
